package com.xszx.controller;

import com.xszx.entity.RoleMenu;
import com.xszx.entity.TMeneu;
import com.xszx.entity.TRoel;
import com.xszx.entity.TUser;
import com.xszx.service.RoleMenuService;
import com.xszx.service.TMeneuService;
import com.xszx.service.TRoelService;
import com.xszx.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
public class RoelController {

    @Autowired
    private TRoelService roelService;

    @Autowired
    RoleMenuService roleMenuService;

    @Autowired
    TMeneuService meneuService;

    /**
     * 遍历所有角色信息
     * @return
     */
    @RequestMapping("/selectRoles")
    @ResponseBody
    public List<TRoel> selectList(){
        List<TRoel> tRoels = roelService.selectRoelList();
        log.info("遍历所有角色：{}",tRoels);
        return tRoels;
    }
    /**
     * 根据rid获取角色
     *
     */
    @RequestMapping("/getRolebyId")
    @ResponseBody
    public TRoel getRolebyId(TRoel tRoel){
        TRoel tRoels = roelService.getRolebyId(tRoel);
        log.info("根据rid获取角色：{}",tRoels);
        return tRoels;
    }

    /**
     * 添加角色
     * @param rname
     * @return
     */
    @ResponseBody
    @RequestMapping("/insertRoel")
    public String insertRoel(@RequestParam("rname") String rname){
        //雪花算法生成id
        long nextId = new SnowFlakeUtil().getNextId();
        TRoel tRoel = new TRoel();
        tRoel.setRid(nextId);
        tRoel.setRname(rname);
        log.info("添加角色：{}",tRoel);
        boolean flag = roelService.insertRoel(tRoel);
        if(flag){
            return "角色列表页面";
        }
        return "添加角色的页面";
    }


    /**
     * 角色修改
     * @param rid
     * @param rname
     * @return
     */
    @RequestMapping("/updateRole")
    public String updateRole(@RequestParam("rid") long rid, @RequestParam("rname") String rname,@RequestParam("menus") String[] menus){
        TRoel tRoel = new TRoel();
        tRoel.setRid(rid);
        tRoel.setRname(rname);
        log.info("修改角色：{}",tRoel);
        boolean flag = roelService.updateRoel(tRoel);
       log.info("方法执行的结果为：{}",flag);
        return "";
    }

    /**
     * 删除角色
     * @param rid
     * @return
     */
    @RequestMapping("/deleteRoel")
    @ResponseBody
    public int deleteRoel(@RequestParam("rid") long rid){
        log.info("根据id删除角色：{}",rid);
        int i = roelService.deleteRoel(rid);
        System.out.println( i> 0 ? "删除成功！" : "删除失败！");
        System.out.println("受影响的行数为：" + i);
        return i;
    }

    /**
     * 根据用户登录账号查询对应角色操作
     */
    @ResponseBody
    @RequestMapping("/selectMenuList")
    public List<TMeneu> selectMenuList(HttpServletRequest request){
        TUser user = (TUser) request.getSession().getAttribute("user");
        log.info("获取到的用户信息是：{}",user);
        //根据登录账号获取对应的角色
       // TRoel role = roelService.getRoelNameByAccount(user.getAccount());

        //根据获取到的角色在中间表中查找对应的数据集
//        List<RoleMenu> roleMenus = roleMenuService.selectRoleMenu(role.getRid());
//        List<TMeneu> tMeneus = new ArrayList<TMeneu>();
//        for (RoleMenu roleMenu : roleMenus) {
            //根据数据集中的mid查找对应的菜单操作信息，存入集合
//            TMeneu tMeneu = meneuService.selectMenuList(roleMenu.getMid());
//            tMeneus.add(tMeneu);

//        }

//        role.setMeneus(tMeneus);
        //log.info("角色对应的操作功能有：{}",role.getMeneus());
        return meneuService.selectMenus();
    }
}
